package com.guigu.qingcheng.servicegoods.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * redis配置类
 */
@Configuration
public class RedisConfig {

    /**
     * 3.创建 RedisTemplate:用于执行 Redis 操作的方法
     */
    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory){
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        //为 key 设置序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //为 value 设置序列化器
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //关联
        template.setConnectionFactory(connectionFactory);
        return template;
    }
}
